package com.uxwine.alarm.main;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.uxwine.alarm.MainActivity;
import com.uxwine.alarm.R;
import com.uxwine.audio.AudioEffect;
import com.uxwine.audio.RingManager;
import com.uxwine.cmm.BaseFragment;
import com.uxwine.util.Log;
import com.uxwine.util.UxToast;
import com.uxwine.view.SlideDialog;
import com.uxwine.view.SlideWindow;
import com.uxwine.view.t.AlarmView;
import com.uxwine.view.t.AlarmView.AlarmHolder;
import com.uxwine.view.t.DialView;
import com.uxwine.view.t.TimeView;

public class MainFragment extends BaseFragment implements AlarmHolder, View.OnClickListener{
	
	LayoutInflater mInflater;
	AudioEffect mAudioEffect;
	MainActivity mAct;
	
	AlarmView mMainView;
	TimeView mtvTime;
	DialView mdvTime;
	ImageView mbtnMore;
	View mLayer;
	AlarmBuilder mBuilder;
	Timer mTimer;
	int[] mnStates;
	int mnCurrent;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mAct = (MainActivity)getActivity();	
		mBuilder = new AlarmBuilder(mAct);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		mMainView = (AlarmView)inflater.inflate(R.layout.fragment_main, container, false);
		mtvTime = (TimeView)mMainView.findViewById(R.id.main_time_view);
		mdvTime = (DialView)mMainView.findViewById(R.id.main_dial_view);
		mbtnMore = (ImageView)mMainView.findViewById(R.id.main_btn_more);
		
		mMainView.setAlarmHolder(this);
		mbtnMore.setOnClickListener(this);
		mBuilder.init(mdvTime, mtvTime);
		
		mHandler.sendEmptyMessage(1);
		
		return mMainView;
	}		
	
	public void showState(boolean bShow)
	{
		mMainView.lock(!bShow);
	}
	
	public void resetBuilder()
	{
		mBuilder.stop();
	}
	
	public void editAlarm(Alarm a)
	{
		if(null == a)
			mBuilder.create().show();
		else
			mBuilder.edit(a).show();
	}
	

	@Override
	public boolean onBackPressed() {
		// TODO Auto-generated method stub
		return super.onBackPressed();
	}
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 1)
			{
				mBuilder.create().show();
			}
	}};
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.main_btn_more)
		{
			mAct.showMore();
		}
	}

	@Override
	public void onLockedClick(AlarmView v) {
		showState(true);
		mAct.showMain(null);
	}

	@Override
	public void onAlarmFling(AlarmView v, int type) {
		
		mBuilder.stop();
		
		if(type == AlarmView.FLING_DOWN)
		{
			mBuilder.reset().show();
			UxToast.sshow(getActivity(), "Alarm has been reset!");
		}
		else if(type == AlarmView.FLING_UP)
		{
			NameDialog dlg = new NameDialog(mAct);
			dlg.setAlarmBuilder(mBuilder);
			
			dlg.show();
		}
		else if(type == AlarmView.FLING_LEFT)
		{
			UxToast.sshow(getActivity(), "Alarm Model Changed!!");
		}
		else if(type == AlarmView.FLING_RIGHT)
		{
			mAct.showMore();			
		}
	}	
}
